package com.csdn.ingo.dao.ctrip.search.dictionary.productscore;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 *
 * @Copyright Copyright (c)2011
 *
 * @Company ctrip.com
 *
 * @Author li_yao
 *
 * @Version 1.0
 *
 * @Create-at 2011-8-5 10:03:15
 *
 * @Modification-history
 * <br>Date					Author		Version		Description
 * <br>----------------------------------------------------------
 * <br>2011-8-5 10:03:15  	li_yao		1.0			Newly created
 */
public class PinyinMapper {

	private static PinyinMapper SINGLETON;

	private String[][] mapper;

	/**
	 * 拼音文件
	 */
	public final static String DATA_FILE_PINYIN="pinyin.txt";

	public static PinyinMapper get() throws IOException{
		if(SINGLETON == null){
			synchronized (PinyinMapper.class) {
				if(SINGLETON == null){
					SINGLETON = new PinyinMapper();
				}
			}
		}
		return SINGLETON;
	}

	private PinyinMapper() throws IOException {
		loadDict();
	}

	private void loadDict() throws IOException{
		//read file and find max code point
		InputStreamReader reader = new InputStreamReader(
				new FileInputStream(DATA_FILE_PINYIN), "utf8");
		BufferedReader br = new BufferedReader(reader);
		String line = null;
		List<String> lines = new ArrayList<String>(10000);
		int maxCode = 0;
		char ch;
		while((line = br.readLine()) != null) {
			lines.add(line);
			ch = line.charAt(0);
			if(ch > maxCode){
				maxCode = ch;
			}
		}
		br.close();
		reader.close();

		//generate mapper by maxCode
		mapper = new String[maxCode + 1][];
		for(String str:lines){
			mapper[str.charAt(0)] = str.substring(2).split(" ");
		}

	}

	public String[] get(char ch){
		return ch > mapper.length ? null : mapper[ch];
	}

	public String[] getQuanpinShoupin(String word){
		if(word == null || word.isEmpty()){
			return null;
		}
		StringBuilder quanpin = new StringBuilder(word.length()*6);
		StringBuilder shoupin = new StringBuilder(word.length());
		boolean hasPinyin = false;
		for(int i = 0; i < word.length(); i++){
			char ch = word.charAt(i);
			String[] py = get(ch);
			if(py == null){//not cn char
				quanpin.append(ch);
				shoupin.append(ch);
			}
			else{//cn char
				hasPinyin = true;
				quanpin.append(py[0]);
				shoupin.append(py[0].charAt(0));
			}
		}
		return hasPinyin ? new String[]{quanpin.toString(),shoupin.toString()}
			: null;
	}

	public String[] getQuanpins(String word){
		if(word == null || word.isEmpty()){
			return null;
		}
		String[] quanpins = new String[word.length()];
		boolean hasCn = false;
		for(int i = 0; i < word.length(); i++){
			char ch = word.charAt(i);
			String[] py = get(ch);
			if(py == null){//not a cn char
				quanpins[i] = String.valueOf(ch);
			}
			else {//cn char
				hasCn = true;
				quanpins[i] = py[0];
			}

		}
		return hasCn ? quanpins : null;
	}


	public boolean matchPinyin(char ch1, char ch2){
		String[] pys1 = get(ch1);
		if(pys1 == null){
			return false;
		}
		String[] pys2 = get(ch2);
		if(pys2 == null){
			return false;
		}
		for(int i = 0; i < pys1.length; i++){
			for(int j = 0; j < pys2.length; j++){
				if(pys1[i].equals(pys2[j])){
					return true;
				}
			}
		}
		return false;
	}


	public static void main(String[] args) throws IOException {
		PinyinMapper py = get();
		String[] s1 = py.get('中');
		s1 = py.get('麇');
		System.out.println(s1[0]);
	}

}
